package jfoo;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.HashMap;
import java.util.Map.Entry;

public class Preferences implements Runnable {

	HashMap<String, String> prefs;
	private static final Preferences preferences = new Preferences();

	private Preferences() {
		new Thread(this).start();
	}

	public static Preferences getInstance() {
		return preferences;
	}

	public HashMap<String, String> getPrefs() {
		return prefs;
	}
	
	public void setValue(String key, String value){
		prefs.put(key, value);
	}

	public String getLastFmLogin() {
		String login = prefs.get("lastFmLogin");
		if (login == null){
			return "";
		}else{
			return login;
		}
	}

	public String getLastFmPassword() {
		String password = prefs.get("lastFmPassword");
		if (password == null){
			return "";
		}else{
			return password;
		}
	}
	
	public String getLibraryFolder(){
		String folder =  prefs.get("libraryFolder");
		if (folder == null){
			return "";
		}else{
			return folder;
		}
	}

	@Override
	public void run() {
		File ft = new File("preferences.data");
		if (ft.exists()) {

			try {
				ObjectInputStream lecteur = new ObjectInputStream(
						new FileInputStream(ft));
				prefs = ((HashMap<String, String>) lecteur.readObject());

			} catch (java.io.InvalidClassException ex) {
				System.out.println("les préférences ne sont plus a jour");
				prefs = new HashMap<String, String>();

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			prefs = new HashMap<String, String>();
		}

	}
}
